﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmThemDen : Form
    {
        CongVanDenDTO _congVanDen = null;

        public CongVanDenDTO CongVanDen
        {
            get { return _congVanDen; }
            set { _congVanDen = value; }
        }
        bool _readOnly = false;

        public bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value; }
        }

        public frmThemDen()
        {
            InitializeComponent();
        }

        void cmbHeThong_Load()
        {
            cmbHeThong.DisplayMember = "Ten";
            cmbHeThong.ValueMember = "ID";
            cmbHeThong.DataSource = HeThongBUS.LoadAll();
            cmbHeThong.SelectedIndex = -1;
        }
        void cmbLoaiDen_Load()
        {
            cmbLoaiDen.DisplayMember = "Ten";
            cmbLoaiDen.ValueMember = "ID";
            cmbLoaiDen.DataSource = LoaiDenBUS.LoadAll();
            cmbLoaiDen.SelectedIndex = -1;
        }
        void cmbNguoi_Load()
        {
            cmbNguoiTiepNhan.DisplayMember = "Ten";
            cmbNguoiTiepNhan.ValueMember = "ID";
            cmbNguoiXuLy.DisplayMember = "Ten";
            cmbNguoiXuLy.ValueMember = "ID";

            List<NguoiDTO> tmp = NguoiBUS.LoadAll();
            tmp.ForEach(GolbalSettings.MarkName);
            cmbNguoiTiepNhan.DataSource = new List<NguoiDTO>(tmp);
            cmbNguoiTiepNhan.SelectedIndex = -1;
            cmbNguoiXuLy.DataSource = tmp;
            cmbNguoiXuLy.SelectedIndex = -1;
        }

        void lstDanhSachNoiGui_Load()
        {
            if (cmbHeThong.SelectedIndex == -1)
            {
                return;
            }
            List<DonViDTO> tmp=new List<DonViDTO>();
            if (rdbTen.Checked)
            {
                tmp = DonViBUS.LoadAll(((int)(cmbHeThong.SelectedValue)), false);
            }
            else if (rdbSoLan.Checked)
            {
                tmp = DonViBUS.LoadAll(((int)(cmbHeThong.SelectedValue)), true);
            }

            foreach (DonViDTO item in _congVanDen.DonVi)
            {
                for (int i = 0; i < tmp.Count; i++)
                {
                    if (item.HeThong.ID == tmp[i].HeThong.ID && item.ID == tmp[i].ID)
                    {
                        tmp.RemoveAt(i);
                    }
                }
            }

            lstDanhSachNoiGui.DisplayMember = "Ten";
            lstDanhSachNoiGui.ValueMember = "ID";
            lstDanhSachNoiGui.DataSource = tmp;

        }




        void Check()
        {
            if (_readOnly)
            {
                return;
            }
            bool result = true;
            if (cmbLoaiDen.SelectedIndex == -1 || txtSoKyHieu.Text.Trim() == "" || txtSTT.Text.Trim() == "")
            {
                result = false;
            }

            int tmp=0;
            if (!int.TryParse(txtSTT.Text.Trim(), out tmp))
            {
                result = false;
            }
            if (tmp <= 0)
            {
                result = false;
            }

            if (result)
            {
                _congVanDen.Loai = ((LoaiDenDTO)cmbLoaiDen.SelectedItem);
                _congVanDen.Stt = tmp;
                _congVanDen.SoKyHieuVanBan = txtSoKyHieu.Text.Trim();
            }

            if (_congVanDen.DonVi.Count == 0)
            {
                result = false;
            }




            if (result)
            {
                btnThem.Enabled = true;
            }
            else
            {
                btnThem.Enabled = false;
            }
        }

        TextBox CloneTextBox(TextBox original)
        {
            TextBox clone = new TextBox();
            clone.Size = original.Size;
            clone.Font = original.Font;
            clone.BackColor = original.BackColor;
            clone.ReadOnly = original.ReadOnly;
            return clone;
        }




        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThemDen_Load(object sender, EventArgs e)
        {
            if (_congVanDen == null)
            {
                _congVanDen = new CongVanDenDTO();

                dtpNgayLuu.Value = DateTime.Today;   
             
                cmbHeThong_Load();
                cmbLoaiDen_Load();
                cmbNguoi_Load();

                rdbTen.Checked = true;

                lstNoiGui.DisplayMember = "Ten";
                lstNoiGui.ValueMember = "ID";
                lstNoiGui.DataSource = _congVanDen.DonVi;

                dtpHanXuLy.Value = DateTime.Today;
                dtpHanXuLy.CustomFormat = "  ";
                dtpHanXuLy.Enabled = false;

                this.Text = "Them cong van den";
                btnThem.Enabled = false;
            }
            else
            {
                if (!_readOnly)
                {
                    _readOnly = true;
                    dtpNgayLuu.Value = _congVanDen.NgayLuu;


                    cmbHeThong_Load();                    
                    cmbLoaiDen_Load();                    
                    cmbNguoi_Load();
                    
                    rdbTen.Checked = true;

                    cmbLoaiDen.SelectedIndex = -1;
                    if (_congVanDen.Loai != null)
                    {
                        for (int i = 0; i < cmbLoaiDen.Items.Count; i++)
                        {
                            if (((LoaiDenDTO)cmbLoaiDen.Items[i]).ID == _congVanDen.Loai.ID)
                            {
                                cmbLoaiDen.SelectedIndex = i;
                            }
                        }
                    }
                    cmbNguoiTiepNhan.SelectedIndex = -1;
                    if (_congVanDen.NguoiNhan != null)
                    {
                        for (int i = 0; i < cmbNguoiTiepNhan.Items.Count; i++)
                        {
                            if (((NguoiDTO)cmbNguoiTiepNhan.Items[i]).ID == _congVanDen.NguoiNhan.ID)
                            {
                                cmbNguoiTiepNhan.SelectedIndex = i;
                            }
                        }
                    }
                    cmbNguoiXuLy.SelectedIndex = -1;
                    if (_congVanDen.NguoiXuLy != null)
                    {
                        for (int i = 0; i < cmbNguoiXuLy.Items.Count; i++)
                        {
                            if (((NguoiDTO)cmbNguoiXuLy.Items[i]).ID == _congVanDen.NguoiXuLy.ID)
                            {
                                cmbNguoiXuLy.SelectedIndex = i;
                            }
                        }
                    }
                    
                    txtSTT.Text = _congVanDen.Stt.ToString();
                    txtSoKyHieu.Text = _congVanDen.SoKyHieuVanBan;
                    txtNoiDung.Text = _congVanDen.NoiDung;
                    txtGhiChu.Text = _congVanDen.GhiChu;


                    lstNoiGui.DisplayMember = "Ten";
                    lstNoiGui.ValueMember = "ID";
                    lstNoiGui.DataSource = _congVanDen.DonVi;

                    if (_congVanDen.NgayXuLy.HasValue)
                    {
                        chkHanXuLy.Checked = true;
                        dtpHanXuLy.Value = _congVanDen.NgayXuLy.Value;
                        dtpHanXuLy.CustomFormat = "d/M/yyyy";
                        dtpHanXuLy.Enabled = true;
                    }
                    else
                    {
                        chkHanXuLy.Checked = false;
                        dtpHanXuLy.Value = DateTime.Today;
                        dtpHanXuLy.CustomFormat = "  ";
                        dtpHanXuLy.Enabled = false;
                    }

                    this.Text = "Sua cong van den";
                    btnThem.Enabled = false;
                    btnThem.Text = "Lưu";
                    btnThem.Click -= btnThem_Click;
                    btnThem.Click += btnLuu_Click;
                    
                    _readOnly = false;
                    Check();
                }
                else
                {
                    txtSTT.ReadOnly = true;
                    txtSoKyHieu.ReadOnly = true;
                    txtNoiDung.ReadOnly = true;
                    txtGhiChu.ReadOnly = true;

                    TextBox txtNgayLuu = CloneTextBox(txtSTT);
                    txtNgayLuu.Location = dtpNgayLuu.Location;
                    txtNgayLuu.Text = _congVanDen.NgayLuu.ToString(dtpNgayLuu.CustomFormat);
                    grbSoKyHieu.Controls.Add(txtNgayLuu);

                    TextBox txtLoai = CloneTextBox(txtSTT);
                    txtLoai.Location = cmbLoaiDen.Location;
                    txtLoai.Text = _congVanDen.Loai.Ten;
                    grbSoKyHieu.Controls.Add(txtLoai);

                    TextBox txtNguoiTiepNhan = CloneTextBox(txtSTT);
                    txtNguoiTiepNhan.Location = cmbNguoiTiepNhan.Location;
                    if (_congVanDen.NguoiNhan != null)
                    {
                        txtNguoiTiepNhan.Text = _congVanDen.NguoiNhan.Ten;
                    }
                    grbTiepNhan.Controls.Add(txtNguoiTiepNhan);

                    TextBox txtNguoiXuLy = CloneTextBox(txtSTT);
                    txtNguoiXuLy.Location = cmbNguoiXuLy.Location;
                    if (_congVanDen.NguoiXuLy != null)
                    {
                        txtNguoiXuLy.Text = _congVanDen.NguoiXuLy.Ten;
                    }
                    grbTiepNhan.Controls.Add(txtNguoiXuLy);

                    TextBox txtNgayXuLy = CloneTextBox(txtSTT);
                    txtNgayXuLy.Location = dtpHanXuLy.Location;
                    if (_congVanDen.NgayXuLy.HasValue)
                    {
                        txtNgayXuLy.Text = _congVanDen.NgayXuLy.Value.ToString(dtpHanXuLy.CustomFormat);
                        chkHanXuLy.Checked = true;
                    }
                    grbTiepNhan.Controls.Add(txtNgayXuLy);

                    


                   


                    txtSTT.Text = _congVanDen.Stt.ToString();
                    txtSoKyHieu.Text = _congVanDen.SoKyHieuVanBan;
                    txtNoiDung.Text = _congVanDen.NoiDung;
                    txtGhiChu.Text = _congVanDen.GhiChu;

                    foreach (Control control in grbSoKyHieu.Controls)
                    {
                        Type tmp = control.GetType();
                        if (tmp.Name != "TextBox" && tmp.Name != "Label")
                        {
                            control.Enabled = false;
                            control.Hide();
                        }
                    }
                    foreach (Control control in grbNoiGui.Controls)
                    {
                        control.Enabled = false;
                        control.Hide();
                    }
                    chkHanXuLy.AutoCheck = false;
                    foreach (Control control in grbTiepNhan.Controls)
                    {
                        Type tmp = control.GetType();
                        if (tmp.Name != "TextBox" && tmp.Name != "Label" && tmp.Name != "CheckBox")
                        {
                            control.Enabled = false;
                            control.Hide();
                        }
                    }
                    TextBox txtNoiGui = CloneTextBox(txtSTT);
                    txtNoiGui.Multiline = true;
                    txtNoiGui.ScrollBars = ScrollBars.Vertical;
                    grbNoiGui.Controls.Add(txtNoiGui);
                    txtNoiGui.Dock = DockStyle.Fill;
                    foreach (DonViDTO tmp in _congVanDen.DonVi)
                    {
                        txtNoiGui.Text += (tmp.Ten + Environment.NewLine);
                    }


                    this.Text = "Xem chi tiet cong van den";
                    btnThem.Hide();
                    btnThem.Click -= btnThem_Click;
                    
                    /////////////////////////
                }
            }
        }

        private void chkHanXuLy_CheckedChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            if (chkHanXuLy.Checked)
            {
                dtpHanXuLy.CustomFormat = @"d/M/yyyy";
                dtpHanXuLy.Enabled = true;
                _congVanDen.NgayXuLy = dtpHanXuLy.Value;
            }
            else
            {
                dtpHanXuLy.CustomFormat = "  ";
                dtpHanXuLy.Enabled = false;
                _congVanDen.NgayXuLy = null;
            }
        }

        private void dtpHanXuLy_ValueChanged(object sender, EventArgs e)
        {
            if (_readOnly || !chkHanXuLy.Checked)
            {
                return;
            }
            _congVanDen.NgayXuLy = dtpHanXuLy.Value;
        }

        private void dtpNgayLuu_ValueChanged(object sender, EventArgs e)
        {
            _congVanDen.NgayLuu = dtpNgayLuu.Value;
        }

        private void btnThemNguoiNhan_Click(object sender, EventArgs e)
        {
            frmCanBo frm = new frmCanBo();
            frm.ShowDialog();
            cmbNguoi_Load();
        }

        private void cmbNguoiTiepNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDen.NguoiNhan = ((NguoiDTO)cmbNguoiTiepNhan.SelectedItem);
        }

        private void cmbNguoiXuLy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDen.NguoiXuLy = ((NguoiDTO)cmbNguoiXuLy.SelectedItem);
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDen.NoiDung = txtNoiDung.Text.Trim();
        }

        private void txtGhiChu_TextChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            _congVanDen.GhiChu = txtGhiChu.Text.Trim();
        }


        private void btnThemLoaiDen_Click(object sender, EventArgs e)
        {
            frmLoaiDen frm = new frmLoaiDen();
            frm.ShowDialog();
            cmbLoaiDen_Load();
        }

        private void cmbLoaiDen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_readOnly)
            {
                return;
            }
            if (cmbLoaiDen.SelectedIndex == -1)
            {
                txtSTT.Text = "";
                txtSoKyHieu.Text = "";
            }
            else
            {
                txtSTT.Text = (1 + CongVanDenBUS.Count((int)(cmbLoaiDen.SelectedValue))).ToString();
                txtSoKyHieu.Text = txtSTT.Text + "_" + GolbalSettings.Abbr(((LoaiDenDTO)(cmbLoaiDen.SelectedItem)).Ten);
            }
            Check();
        }

        private void txtSTT_TextChanged(object sender, EventArgs e)
        {
            if (cmbLoaiDen.SelectedIndex == -1)
            {
                Check();
                return;
            }
            txtSoKyHieu.Text = txtSTT.Text + "_" + GolbalSettings.Abbr(((LoaiDenDTO)(cmbLoaiDen.SelectedItem)).Ten);
            Check();
        }

        private void txtSoKyHieu_TextChanged(object sender, EventArgs e)
        {
            Check();
        }

        private void rdbTen_CheckedChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiGui_Load();
        }

        private void rdbSoLan_CheckedChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiGui_Load();
        }

        private void cmbHeThong_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDanhSachNoiGui_Load();
        }

        private void btnThemNoiGui_Click(object sender, EventArgs e)
        {
            frmDonVi frm = new frmDonVi();
            frm.ShowDialog();
            _congVanDen.DonVi.Clear();
            lstDanhSachNoiGui_Load();
            ((CurrencyManager)lstNoiGui.BindingContext[lstNoiGui.DataSource]).Refresh();
            Check();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (lstDanhSachNoiGui.SelectedIndex == -1)
            {
                return;
            }
            _congVanDen.DonVi.Add((DonViDTO)(lstDanhSachNoiGui.SelectedItem));
            ((CurrencyManager)lstNoiGui.BindingContext[lstNoiGui.DataSource]).Refresh();
            lstDanhSachNoiGui_Load();
            Check();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (lstNoiGui.SelectedIndex == -1)
            {
                return;
            }
            _congVanDen.DonVi.Remove((DonViDTO)(lstNoiGui.SelectedItem));
            ((CurrencyManager)lstNoiGui.BindingContext[lstNoiGui.DataSource]).Refresh();
            lstDanhSachNoiGui_Load();
            Check();
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            _congVanDen.DonVi.Clear();
            ((CurrencyManager)lstNoiGui.BindingContext[lstNoiGui.DataSource]).Refresh();
            lstDanhSachNoiGui_Load();
            Check();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (CongVanDenBUS.Add(_congVanDen))
                {
                    cmbLoaiDen_Load();
                    cmbNguoi_Load();
                    lstDanhSachNoiGui_Load();
                    chkHanXuLy.Checked = false;
                    txtNoiDung.Text = "";
                    txtGhiChu.Text = "";

                    if (!CongVanDenBUS.AddRelationship(_congVanDen))
                    {
                        MessageBox.Show("Không thể thêm một số nơi gửi.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }

                }
                else
                {
                    MessageBox.Show("Không thể thêm công văn mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (CongVanDenBUS.Edit(_congVanDen))
                {

                    if (!(CongVanDenBUS.DeleteRelationship(_congVanDen.ID)&&CongVanDenBUS.AddRelationship(_congVanDen)))
                    {
                        MessageBox.Show("Không thể cập nhật một số nơi gửi.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        MessageBox.Show("Đã cập nhật dữ liệu.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }


                }
                else
                {
                    MessageBox.Show("Không thể cập nhật dữ liệu.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }



    }
}
